C++从键盘输入多行数据 您所在的位置:网站首页 cpp cin函数 C++从键盘输入多行数据

C++从键盘输入多行数据

2024-06-28 10:50| 来源: 网络整理| 查看: 265

C++从键盘输入多行数据 1.cincin>>cin.getline(数组名,长度,结束符)cin.get(数组名,长度,结束符)关于cin、cin.get()、cin.get(ch)返回值的问题 2.std::getline3.std::istringstream4.istringstream、ostringstream、stringstream 类5.练习题

1.cin

输入原理: 程序的输入都建有一个缓冲区,即输入缓冲区。一次输入过程是这样的,当一次键盘输入结束时会将输入的数据存入输入缓冲区,而cin函数直接从输入缓冲区中取数据。正因为cin函数是直接从缓冲区取数据的,所以有时候当缓冲区中有残留数据时,cin函数会直接取得这些残留数据而不会请求键盘输入

C++ 读取键盘输入(cin/cin.getline()/cin.get()/cin.clear())

cin>>

http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/ 该操作符是根据后面变量的类型读取数据。

看一下cin>>支持读取的数据类型有哪些

//cin>>支持的类型,这里没有char&类型,但是实际上cin是可以一次读取一个字符的 //具体的可以看看的下面的链接 //https://docs.microsoft.com/en-us/cpp/standard-library/istream-operators?view=vs-2019 istream& operator>> (bool& val); istream& operator>> (short& val); istream& operator>> (unsigned short& val); istream& operator>> (int& val); istream& operator>> (unsigned int& val); istream& operator>> (long& val); istream& operator>> (unsigned long& val); istream& operator>> (long long& val); istream& operator>> (unsigned long long& val); istream& operator>> (float& val); istream& operator>> (double& val); istream& operator>> (long double& val); //数组 istream& operator>> (void*& val); //stream buffers istream& operator>> (streambuf* sb ); //manipulators istream& operator>> (istream& (*pf)(istream&)); istream& operator>> (ios& (*pf)(ios&)); istream& operator>> (ios_base& (*pf)(ios_base&));

输入结束条件 :遇到Enter、Space、Tab键。 对结束符的处理 :丢弃缓冲区中使得输入结束的结束符(Enter、Space、Tab)

使用cin读取键盘输入的字符串的实验:

#include //test input int main() { using namespace std; char word[10]; //cin读取字符串时以空白符为界,表明cin只会读第一个单词,遇到空白符后结束 //后面的字符都还在输入缓冲中 cin>>word; cout cin>>input;//读取第一个单词 cin.get(ch);//捕获空白符,如' ','\t','\n' //cout using namespace std; char c1; char c2; //1.无参数的cin.get(),记住cin.get()返回值为int类型,所以这里有类型转换 c1=cin.get(); cout int c; cout char ch[20]; cout string str; cout std::ifstream is ("test.txt", std::ifstream::binary); if (is) { // get length of file: //1.设置流中下一次提取字符的位置为流的结尾处 is.seekg (0, is.end); //2.返回当前字符在流中的位置,因为前面将位置设置为了结尾处, //所以这里返回的就是流中数据的长度 int length = is.tellg(); //3.设置流中下一次提取字符的位置为流的开始处 is.seekg (0, is.beg); // 申请能容纳流中完整数据的动态数组 char * buffer = new char [length]; //从流中读取数据 is.read (buffer,length); //关闭文件流 is.close(); //将buffer的数据写入标准输出cout std::cout.write (buffer,length); //释放内存 delete[] buffer; } return 0; } 成员函数operator>>声明如下: 继承自std::istream //cin>>支持的类型,这里没有char&类型,但是实际上cin是可以一次读取一个字符的 //具体的可以看看的下面的链接 //https://docs.microsoft.com/en-us/cpp/standard-library/istream-operators?view=vs-2019 istream& operator>> (bool& val); istream& operator>> (short& val); istream& operator>> (unsigned short& val); istream& operator>> (int& val); istream& operator>> (unsigned int& val); istream& operator>> (long& val); istream& operator>> (unsigned long& val); istream& operator>> (long long& val); istream& operator>> (unsigned long long& val); istream& operator>> (float& val); istream& operator>> (double& val); istream& operator>> (long double& val); //数组 istream& operator>> (void*& val); //stream buffers (2) istream& operator>> (streambuf* sb ); //manipulators (3) istream& operator>> (istream& (*pf)(istream&)); istream& operator>> (ios& (*pf)(ios&)); istream& operator>> (ios_base& (*pf)(ios_base&));

关于operator>>的返回值, 1.返回值为(*this) 2.内部状态错误时,会设置标志位

标志错误eofbit输入序列中没有更多可用字符(到达文件末尾)。failbit没有提取任何字符,或者提取的字符不能解释为适当类型的有效值。badbit流上的错误(例如,当此函数捕获内部操作引发的异常时)。 ignore:提取并丢弃字符 从输入序列中提取字符并丢弃它们,直到提取出n个字符或一个等于delim的终止字符为止。 http://www.cplusplus.com/reference/istream/istream/ignore/ istream& ignore (streamsize n = 1, int delim = EOF); 参数:需要提取并丢弃字符串的最大长度,终止字符delim #include #include using namespace std; int main(){ string test = "-123 9.87 welcome to, 989, test!"; istringstream strm(test); //创建存储 test 的副本的 stringstream 对象 int i; float f; strm >> i; cout string str="this is a string"; //构造一个istringstream对象is istringstream is(str); string s; //类似cin,这里可以is流中读取字符串,遇到空格,表示读取一个字符完成 //读到结尾后,会设置标志位,然后会利用其成员函数operator bool实现类型转换,跳出循环 while(is>>s) { cout //清除上一次的istr到达结尾后设置的错误标志位,如果不清除,那下一次的while(istr >> str)会直接退出 istr.clear(); istr.str(line);//把line中的字符串存入字符串流中 //这里可以看看错误标志位的值 //cout std::stringstream stream; string str; while(1) { //clear(),这个名字让很多人想当然地认为它会清除流的内容。 //实际上,它并不清空任何内容,它只是重置了流的状态标志而已! stream.clear(); // 去掉下面这行注释,清空stringstream的缓冲,每次循环内存消耗将不再增加! //stream.str(""); streamstr; //测试输出每次循环,你的内存消耗增加了多少! cout //构造一个istringstream对象is istringstream is(str); string s; //类似cin,这里可以is流中读取字符串,遇到空格,表示读取一个字符完成 //读到结尾后,会设置标志位,然后会利用其成员函数operator bool实现类型转换,跳出循环 while(is>>s) { data.push_back(stoi(s)); } } cout if(s=="") return ""; istringstream istr(s); string res,temp; while(istr>>temp){ reverse(temp.begin(),temp.end()); res+=temp; res+=" "; } //删除尾部多余的一个空格 res.pop_back(); return res; } }; 分割字符串

给定一个字符串,例如"12,14,#,#,13,43,2324,#,#,#,232,#,432,#,#" 去掉其中所有的逗号,分割后的字符串保存到vector< string >中

方法一:利用istringstream和getline

#include #include//getline,string #include #include//istringstream using namespace std; vector split1(string& str){ //用str构造字符流istr istringstream istr(str); string temp; vector result; //使用getline从istr流中获取一行时以','作为截至符 while(getline(istr,temp,',')){ result.push_back(temp); } return result; } int main(){ string str="12,14,#,#,13,43,2324,#,#,#,232,#,432,#,#"; vector res=split1(str); for(auto& i:res) cout}; vector res; //为了利用strtok(char* str,char* delim)函数,先将string复制到char数组 char* cstr=new char[str.length()+1]; strcpy(cstr,str.c_str()); char* cdelim=new char[delim.length()+1]; strcpy(cdelim,delim.c_str()); char* p=strtok(cstr,cdelim); while(p){ string s=p; res.push_back(s); p=strtok(NULL,cdelim); } delete cstr; delete cdelim; return res; } int main(){ string str="12,14,#,#,13,43,2324,#,#,#,232,#,432,#,#"; vector res=split2(str,","); for(auto& i:res) cout


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有